﻿(function ($) {
    $.extend($.ui, { DateTimepicker: {} });

    $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
    $.datepicker._updateDatepicker = function (inst) {
        $.datepicker._base_updateDatepicker(inst);
        var val = inst.input.val();
        if (inst.input.hasClass("datetimepicker") == false) return;
        if($("#"+inst.id).hasClass("notime") == false){
               var bottomLayer = $(".ui-datepicker").append("<div class='.datetimepicker' />");

               bottomLayer.append('');
               if($('#is_corptrip').length){
                   var h_hourHTML = '';
                   for(var i=0;i<=24;i++){
                       var s_select='';
                       if(i==9 && inst.id == 'start_time'){
                           s_select = "selected='"+s_select+"'";
                       }else if(i==18 && inst.id == 'end_time'){
                           s_select = "selected='"+s_select+"'";
                       }
                       h_hourHTML += "<option value='"+(i<10 ? 0:'')+i+":00' "+s_select+">"+(i<10 ? 0:'')+i+":00</option>";
                   }
                   hourHTML = $("<select class='dateimepicker-hour' onchange='$.datetimepicker.isDirty = true;'>"+h_hourHTML+"</select>");
               }else{
                   //扩展结束时间默认为下午 by coldarmy
                   morningselect = " selected='selected'";
                   afterselect = '';
                   if(inst.id == 'end_time') {
                       morningselect = '';
                       afterselect = " selected='selected'";
                   }
                   hourHTML = $("<select class='dateimepicker-hour' onchange='$.datetimepicker.isDirty = true;'><option value='09:00'"+morningselect+">09:00</option><option value='14:00'>14:00</option><option value='18:00'"+afterselect+">18:00</option></select>");
               }


               bottomLayer.append(hourHTML);
               bottomLayer.append("时");
        }

        //bottomLayer.append(html2);
        //bottomLayer.append("분 ");
        //bottomLayer.append(html3);
        //bottomLayer.append("초 ");

        $.datetimepicker.setTime(val, inst.input);
    };


    function DateTimePicker(options) {

        this.defaultDateTimePicker =
            {
                showTimePicker: false,
                time_format: 'yy'
            };

        hourHTML = $("<select class='dateimepicker-hour' onchange='$.datetimepicker.isDirty = true;'></select>");
        //minutsHTML = $("<select class='dateimepicker-minuts' onchange='$.datetimepicker.isDirty = true;'></select>");
        //secondHTML = $("<select class='dateimepicker-second' onchange='$.datetimepicker.isDirty = true;'></select>");

        //html1 = hourHTML.append("<option value='09:00'>09:00</option><option value='14:00'>14:00</option><option value='18:00'>18:00</option>");

        //for (i = 1; i <= 24; i++) html1 = hourHTML.append("<option>" + i + "</option");
        //for (i = 0; i < 60; i = i + 5) html2 = minutsHTML.append("<option>" + i + "</option");
        //for (i = 0; i < 60; i = i + 5) html3 = secondHTML.append("<option>" + i + "</option");
    }

    $.fn.extend({
        datetimepicker: function (options) {
            $.datetimepicker._attach(this, options);
        }
    });

    DateTimePicker.prototype = {
        _initTimePicker: function () {

        },

        isDirty: false,

        innerOption: function ($this) {

            return {
                showOn: "focus",
                buttonImage: "../../page/img/calendar.gif",
                buttonImageOnly: true,
                changeMonth: true,
                changeYear: true,
                autoSize: true,
                onSelect: function (a, b) {
                    var hour = $('.ui-datepicker .dateimepicker-hour').val();
                    //var minuts = $('.ui-datepicker .dateimepicker-minuts').val();
                    //var second = $('.ui-datepicker .dateimepicker-second').val();

                    //var time = hour + ":" + minuts + ":" + second;


                    if(hour){
                        var time = hour;
                        $this.val($this.val() + " " + time);
                    }else{
                        $this.val($this.val());
                    }


                    $.datetimepicker.setTime($this.val(), $this);

                    $.datetimepicker.reset();

                    $.datetimepicker.after_set($this);
                },
                onClose: function (a, b) {
                    if ($.datetimepicker.isDirty) {
                        $this.val($.datepicker._formatDate(b));

                        var hour = $('.ui-datepicker .dateimepicker-hour').val();
                        //var minuts = $('.ui-datepicker .dateimepicker-minuts').val();
                        //var second = $('.ui-datepicker .dateimepicker-second').val();
                        //var time = hour + ":" + minuts + ":" + second;
                        if(hour){
                            var time = hour;
                            $this.val($this.val() + " " + time);
                        }else{
                            $this.val($this.val());
                        }

                        $.datetimepicker.after_set($this);
                    }
                }
            };
        },

        _attach: function ($this, options) {
            $this.datepicker($.fn.extend(this.innerOption($this), options))
                .addClass("datetimepicker");

            this.setTime($this.val(), $this);

        },

        reset: function () {
            this.isDirty = false;
        },

        after_set: function ($this) {

        },

        setTime: function (format, $this) {
            var arrStr = format.split(' ');

            var hour = '09:00';
            //var minuts = 0;
            //var second = 0;

            if (arrStr.length > 1) {
                var strTime = arrStr[1];
                var arr = strTime.split(':');
                if (arr.length > 0) {
                    hour = arr[0];
                    //if (arr.length > 1) minuts = arr[1];
                    //if (arr.length > 2) second = arr[2];
                }
            }

            //$('.ui-datepicker .dateimepicker-hour').val(hour);去掉默认选择设定，by coldarmy 20120628
            //$('.ui-datepicker .dateimepicker-minuts').val(minuts);
            //$('.ui-datepicker .dateimepicker-second').val(second);
        }
    };


    $.datetimepicker = new DateTimePicker();
    $.datetimepicker._initTimePicker();

} (jQuery));
